home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / infosrvr / dev / scott / WWW / NextStep / Implementation / old / HTTCP.c < prev    next >
C/C++ Source or Header  |  1991-09-11  |  7KB  |  277 lines

  1. /*            Generic Communication Code        HTTCP.c
  2. **            ==========================
  3. **
  4. **    This code is in common between client and server sides.
  5. */
  6.  
  7.  
  8. #include "HTUtils.h"
  9. #include "tcp.h"        /* Defines SHORT_NAMES if necessary */
  10. #ifdef SHORT_NAMES
  11. #define HTInetStatus        HTInStat
  12. #define HTInetString         HTInStri
  13. #endif
  14.  
  15.  
  16. /*    Module-Wide variables
  17. */
  18.  
  19. PRIVATE char *hostname=0;        /* The name of this host */
  20.  
  21.  
  22. /*    PUBLIC VARIABLES
  23. */
  24.  
  25. PUBLIC struct sockaddr_in HTHostAddress;    /* The internet address of the host */
  26.                     /* Valid after call to HTHostName() */
  27.  
  28. /*    Encode INET status (as in sys/errno.h)              inet_status()
  29. **    ------------------
  30. **
  31. ** On entry,
  32. **    where        gives a description of what caused the error
  33. **    global errno    gives the error number in the unix way.
  34. **
  35. ** On return,
  36. **    returns        a negative status in the unix way.
  37. */
  38. #ifdef vms
  39. extern int uerrno;    /* Deposit of error info (as perr errno.h) */
  40. extern int vmserrno;    /* Deposit of VMS error info */
  41. extern volatile noshare int errno;  /* noshare to avoid PSECT conflict */
  42. #else
  43. #ifndef errno
  44. extern int errno;
  45. #endif
  46. #endif
  47.  
  48. #ifndef VM
  49. #ifndef vms
  50. #ifndef NeXT
  51. extern char *sys_errlist[];        /* see man perror on cernvax */
  52. extern int sys_nerr;
  53. #endif
  54. #endif
  55. #endif
  56.  
  57.  
  58. /*    Report Internet Error
  59. **    ---------------------
  60. */
  61. #ifdef __STDC__
  62. PUBLIC int HTInetStatus(char *where)
  63. #else
  64. PUBLIC int HTInetStatus(where)
  65.     char    *where;
  66. #endif
  67. {
  68.     CTRACE(tfp, "TCP: Error %d in `errno' after call to %s() failed.\n\t%s\n",
  69.         errno,  where,
  70. #ifdef VM
  71.         "(Error number not translated)");    /* What Is the VM equiv? */
  72. #define ER_NO_TRANS_DONE
  73. #endif
  74. #ifdef vms
  75.         "(Error number not translated)");
  76. #define ER_NO_TRANS_DONE
  77. #endif
  78. #ifdef NeXT
  79.         strerror(errno));
  80. #define ER_NO_TRANS_DONE
  81. #endif
  82.  
  83. #ifndef ER_NO_TRANS_DONE
  84.         errno < sys_nerr ? sys_errlist[errno] : "Unknown error" );
  85. #endif
  86.  
  87.  
  88. #ifdef vms
  89.     CTRACE(tfp, "         Unix error number (uerrno) = %ld dec\n", uerrno);
  90.     CTRACE(tfp, "         VMS error (vmserrno)       = %lx hex\n", vmserrno);
  91. #endif
  92.     return -errno;
  93. }
  94.  
  95.  
  96. /*    Parse a cardinal value                       parse_cardinal()
  97. **    ----------------------
  98. **
  99. ** On entry,
  100. **    *pp        points to first character to be interpreted, terminated by
  101. **            non 0:9 character.
  102. **    *pstatus    points to status already valid
  103. **    maxvalue    gives the largest allowable value.
  104. **
  105. ** On exit,
  106. **    *pp        points to first unread character
  107. **    *pstatus    points to status updated iff bad
  108. */
  109. #ifdef __STDC__
  110. PUBLIC unsigned int HTCardinal(int *pstatus,
  111.     char        **pp,
  112.     unsigned int    max_value)
  113. #else
  114. PUBLIC unsigned int HTCardinal(pstatus, pp, max_value)
  115.    int            *pstatus;
  116.    char            **pp;
  117.    unsigned int        max_value;
  118. #endif
  119. {
  120.     int   n;
  121.     if ( (**pp<'0') || (**pp>'9')) {        /* Null string is error */
  122.     *pstatus = -3;  /* No number where one expeceted */
  123.     return 0;
  124.     }
  125.  
  126.     n=0;
  127.     while ((**pp>='0') && (**pp<='9')) n = n*10 + *((*pp)++) - '0';
  128.  
  129.     if (n>max_value) {
  130.     *pstatus = -4;  /* Cardinal outside range */
  131.     return 0;
  132.     }
  133.  
  134.     return n;
  135. }
  136.  
  137.  
  138. /*    Produce a string for an inernet address
  139. **    ---------------------------------------
  140. **
  141. ** On exit,
  142. **    returns    a pointer to a static string which must be copied if
  143. **        it is to be kept.
  144. */
  145. #ifdef __STDC__
  146. PUBLIC const char * HTInetString(struct sockaddr_in* sin)
  147. #else
  148. PUBLIC char * HTInetString(sin)
  149.     struct sockaddr_in *sin;
  150. #endif
  151. {
  152.     static char string[16];
  153.     sprintf(string, "%d.%d.%d.%d",
  154.         (int)*((unsigned char *)(&HTHostAddress.sin_addr)+0),
  155.         (int)*((unsigned char *)(&HTHostAddress.sin_addr)+1),
  156.         (int)*((unsigned char *)(&HTHostAddress.sin_addr)+2),
  157.         (int)*((unsigned char *)(&HTHostAddress.sin_addr)+3));
  158.     return string;
  159. }
  160.  
  161.  
  162. /*    Parse an internet node address and port
  163. **    ---------------------------------------
  164. **
  165. ** On entry,
  166. **    str    points to a string with a node name or number,
  167. **        with optional trailing colon and port number.
  168. **    sin    points to the binary internet address field.
  169. **
  170. ** On exit,
  171. **    *sin    is filled in. If no port is specified in str, that
  172. **        field is left unchanged in *sin.
  173. */
  174. #ifdef __STDC__
  175. PUBLIC int HTParseInet(struct sockaddr_in* sin, const char *str)
  176. #else
  177. PUBLIC int HTParseInet(sin, str)
  178.     struct sockaddr *sin;
  179.     char *str;
  180. #endif
  181. {
  182.     char *port;
  183.     char host[256];
  184.     struct hostent  *phost;    /* Pointer to host - See netdb.h */
  185.     strcpy(host, str);        /* Take a copy we can mutilate */
  186.  
  187.  
  188.  
  189. /*    Parse port number if present
  190. */    
  191.     if (port=index(host, ':')) {
  192.         *port++ = 0;        /* Chop off port */
  193.         if (port[0]>='0' && port[0]<='9') {
  194.         sin->sin_port = htons(strtol(port, (char**)0 , 10));
  195.     } else {
  196. #ifdef SUPPRESS        /* 1. crashes!?!.  2. Not recommended */
  197.         struct servent * serv = getservbyname(port, (char*)0);
  198.         if (serv) sin->sin_port = serv->s_port;
  199.         else if (TRACE) printf("TCP: Unknown service %s\n", port);
  200. #endif
  201.     }
  202.     }
  203.  
  204. /*    Parse host number if present
  205. */  
  206.     if (*host>='0' && *host<='9') {   /* Numeric node address: */
  207.     sin->sin_addr.s_addr = inet_addr(host); /* See arpa/inet.h */
  208.  
  209.     } else {            /* Alphanumeric node name: */
  210.     phost=gethostbyname(host);    /* See netdb.h */
  211.     if (!phost) {
  212.         if (TRACE) printf(
  213.             "HTTPAccess: Can't find internet node name `%s'.\n",host);
  214.         return -1;  /* Fail? */
  215.     }
  216.     memcpy(&sin->sin_addr, phost->h_addr, phost->h_length);
  217.     }
  218.  
  219.     if (TRACE) printf( 
  220.     "TCP: Parsed address as port %d, IP address %d.%d.%d.%d\n",
  221.         (unsigned int)ntohs(sin->sin_port),
  222.         (int)*((unsigned char *)(&sin->sin_addr)+0),
  223.         (int)*((unsigned char *)(&sin->sin_addr)+1),
  224.         (int)*((unsigned char *)(&sin->sin_addr)+2),
  225.         (int)*((unsigned char *)(&sin->sin_addr)+3));
  226.  
  227.     return 0;    /* OK */
  228. }
  229.  
  230.  
  231.  
  232. /*    Derive the name of the host on which we are
  233. **    -------------------------------------------
  234. **
  235. */
  236. #ifdef __STDC__
  237. PRIVATE void get_host_details(void)
  238. #else
  239. PRIVATE void get_host_details()
  240. #endif
  241.  
  242. #ifndef MAXHOSTNAMELEN
  243. #define MAXHOSTNAMELEN 64        /* Arbitrary limit */
  244. #endif
  245.  
  246. {
  247.     char name[MAXHOSTNAMELEN+1];    /* The name of this host */
  248.     struct hostent * phost;        /* Pointer to host -- See netdb.h */
  249.     int namelength = sizeof(name);
  250.     
  251.     if (hostname) return;        /* Already done */
  252.     gethostname(name, namelength);    /* Without domain */
  253.     CTRACE(tfp, "TCP: Local host name is %s\n", name);
  254.     phost=gethostbyname(name);        /* See netdb.h */
  255.     if (!phost) {
  256.     if (TRACE) printf(
  257.         "TCP: Can't find my own internet node address for `%s'!!\n",
  258.         name);
  259.     return;  /* Fail! */
  260.     }
  261.     StrAllocCopy(hostname, phost->h_name);
  262.     memcpy(&HTHostAddress, &phost->h_addr, phost->h_length);
  263.     if (TRACE) printf("     Name server says that is `%s' = %s\n",
  264.         hostname, HTInetString(&HTHostAddress));
  265. }
  266.  
  267. #ifdef __STDC__
  268. PUBLIC const char * HTHostName(void)
  269. #else
  270. PUBLIC char * HTHostName()
  271. #endif
  272. {
  273.     get_host_details();
  274.     return hostname;
  275. }
  276.  
  277.